// AnimatedLabel.cpp : implementation file
//

#include "stdafx.h"
#include "AnimatedLabel.h"
#include "global.h"
#include "MFCPlayer.h"
#include "MFCPlayerDlg.h"
#include "BitmapBtn.h"
// #include "DlgWndPop.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define IDT_ANIMATED			1
#define IDT_ADTEXTSHOWTIMER		2
#define LABEL_LENGTH	180
/////////////////////////////////////////////////////////////////////////////
// CAnimatedLabel

CAnimatedLabel::CAnimatedLabel()
{
	m_nTextPos = m_nMaxTextLen = 0;
	m_bIsEdit = FALSE;
	m_bEditing = FALSE;
	m_bIsButton = FALSE;
	m_bPassword = FALSE;
	m_bAsBtnClick = FALSE;
	m_bAnimated = FALSE;
	m_bAnimatedDone = FALSE;
	m_nAnimatePos = 0;
	m_strLabel2 = _T("");
	m_nScrope = DT_CENTER | DT_SINGLELINE | DT_VCENTER;
	m_nAdTextIndex = 0;
	m_bShowSpeedFlag = 0;
}

CAnimatedLabel::~CAnimatedLabel()
{
}


BEGIN_MESSAGE_MAP(CAnimatedLabel, CBitmapBtn)
	//{{AFX_MSG_MAP(CAnimatedLabel)
	ON_WM_PAINT()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_RBUTTONUP()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAnimatedLabel message handlers

void CAnimatedLabel::OnPaint() 
{
	PAINTSTRUCT ps;
	CDC* pDC = BeginPaint(&ps);
//	CPaintDC dc(this); // device context for painting
//	if(m_pic != NULL && !(m_bEditing && m_MouseOnButton))
	if(m_bAsBtnClick && m_MouseOnButton)
	{
		CRect rc;
		GetClientRect(&rc);
		CGlobal::DrawBitmap(pDC, (HBITMAP)m_bmp, rc, FALSE);

		CString strShow = m_strLabel;
		if (m_strLabel3.IsEmpty() == FALSE)
		{
			m_strLabel3.Insert(0, _T("  "));
			strShow += m_strLabel3;
		}
		strShow.Delete(0, m_nTextPos);
		pDC->SetBkMode(TRANSPARENT);
		
		LOGFONT lf;
		m_font.GetLogFont(&lf);
		lf.lfUnderline = TRUE;
		CFont ft;
		ft.CreateFontIndirect(&lf);
		HFONT hfontOld = (HFONT)pDC->SelectObject(&ft);
		pDC->SetTextColor(m_clrText);

		pDC->DrawText(strShow, strShow.GetLength(), rc, m_nScrope);
		pDC->SelectObject(hfontOld);
	}
	else if(!(m_bEditing && m_MouseOnButton))
	{
		CRect rc;
		GetClientRect(&rc);
		CGlobal::DrawBitmap(pDC, (HBITMAP)m_bmp, rc, FALSE);

		CString strShow = m_strLabel;
		if (m_strLabel3.IsEmpty() == FALSE)
		{
			strShow += _T("  ");
			strShow += m_strLabel3;
		}

		strShow.Delete(0, m_nTextPos);
		pDC->SetBkMode(TRANSPARENT);
		HFONT hfontOld = (HFONT)pDC->SelectObject(&m_font);
		pDC->SetTextColor(m_clrText);
		if(m_bIsEdit)
			rc.DeflateRect(8, 0, 8, 0);
		if(m_bPassword)
		{
			int nLen = strShow.GetLength();
			strShow = CString('*', nLen);
		}
		if(!m_strLabel2.IsEmpty())
		{
			rc.DeflateRect(0, 0, 5, 0);
			int nlabel2Scropt = DT_VCENTER | DT_SINGLELINE | DT_CENTER;
			CRect rLabel2 = rc;//CRect(rc.Width() - LABEL_LENGTH, rc.top, LABEL_LENGTH, rc.bottom);
			rLabel2.left = rc.Width() - LABEL_LENGTH;
			pDC->DrawText(m_strLabel2, m_strLabel2.GetLength(), rLabel2, nlabel2Scropt);
			rc.DeflateRect(10, 0, LABEL_LENGTH + 8, 0);
		}
//		else 
		if(!m_bAnimated)
		{
			pDC->DrawText(strShow, strShow.GetLength(), rc, m_nScrope);
		}
		else
		{
//			LOGFONT lf;
//			m_font.GetLogFont(&lf);
			if(m_strLabel2.IsEmpty())
				rc.DeflateRect(10, 0, 8, 0);
			int nStrLen = strShow.GetLength() * 7;
			m_nAnimatePos += 2 ;
			int nScrope = m_nScrope;
			if(m_nAnimatePos <= nStrLen)
			{
				rc.left = rc.right;
				rc.DeflateRect( -1 * m_nAnimatePos, 0, 0, 0);
				nScrope = DT_VCENTER | DT_SINGLELINE | DT_LEFT;
			}
			else
			{
				rc.DeflateRect(0, 0, m_nAnimatePos - nStrLen, 0);
				nScrope = DT_VCENTER | DT_SINGLELINE | DT_RIGHT;
//				if(m_nAnimatePos - nStrLen > )
				if(rc.Width() <= 0)
				{
					m_nAnimatePos = 0;
					m_bAnimatedDone = TRUE;
				}
			}
			if(rc.left < 10)
				rc.left = 10;
			pDC->DrawText(strShow, strShow.GetLength(), rc, nScrope);
		}

		pDC->SelectObject(hfontOld);
	}
	EndPaint(&ps);
}

void CAnimatedLabel::SetAnimated(BOOL bAnimate)
{
	m_bAnimated = bAnimate;
	if(m_bAnimated)
	{
		SetTimer(IDT_ANIMATED, 100, NULL);
	}
	else
	{
		KillTimer(IDT_ANIMATED);
	}

}

BOOL CAnimatedLabel::GetAnimated()
{
	return m_bAnimatedDone;
}

void CAnimatedLabel::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent == IDT_ANIMATED)
	{
		CRect rc;
		GetClientRect(rc);
		if(!m_strLabel2.IsEmpty())
			rc.DeflateRect(0, 0, LABEL_LENGTH, 0);
		InvalidateRect(rc, FALSE);
	}
	else if(IDT_ADTEXTSHOWTIMER == nIDEvent)
	{
		//CJiaFilmPlayerDlg* pDlg = (CJiaFilmPlayerDlg*) GetParent();
		CMFCPlayerDlg* pDlg = (CMFCPlayerDlg*) (theApp.m_pMainWnd);
		if(pDlg != NULL && pDlg->m_hWnd != NULL)
			pDlg->SetAdviseText(m_nAdTextIndex);
	}
	CBitmapBtn::OnTimer(nIDEvent);
}

void CAnimatedLabel::OnLButtonUp(UINT nFlags, CPoint point)
{
	CRect r;
	GetClientRect(&r);

	if (m_bAsBtnClick)
	{
		CDC * pDC = GetDC();
		CSize oSize = pDC->GetTextExtent(this->m_strLabel);
		if(m_nScrope & DT_RIGHT)
			r.left = r.right - oSize.cx;
		else if(m_nScrope & DT_CENTER)
		{
			int a = (r.left+r.right)/2 - (oSize.cx/2);
			r.left = a;
			r.right = r.left + oSize.cx;
		}
		else if(m_nScrope & DT_LEFT)
		{
			r.right -= oSize.cx; 
		}
		ReleaseDC(pDC);
	}
	

	if((m_bIsEdit || m_bAsBtnClick) && r.PtInRect(point))
	{
		Invalidate(FALSE);
#if 0
		CJiaFilmPlayerDlg* pDlg = (CJiaFilmPlayerDlg*) GetParent();
		pDlg->LinkText(m_nID, m_strIDName);
#else
		void * pParent = GetParent();
		if (theApp.m_pMainWnd == pParent) 
		{
			CJiaFilmPlayerDlg* pDlg = (CJiaFilmPlayerDlg*) pParent;
			pDlg->LinkText(m_nID, m_strIDName);
		}
		else
		{
			CDlgWndPop* pDlg = (CDlgWndPop*)pParent;
			pDlg->LinkText(m_nID, m_strIDName);
		}

#endif
	}
	CBitmapBtn::OnLButtonUp(nFlags, point);
}

void CAnimatedLabel::CopyFrom(CRect r, CBitmap &bmp)
{
	CDC* dc = GetDC();

	CGlobal::CopyBitmap(dc, m_bmp, bmp, r);
}

void CAnimatedLabel::LoadText(CString strBmp)
{
	m_bmp.DeleteObject();
	CSize size;
	if(!strBmp.IsEmpty())
	{
		HBITMAP hBmp = ::LoadBitmap(theApp.m_hRes, MAKEINTRESOURCE(atoi(strBmp)));
		m_bmp.Attach(hBmp);
	}
}

void CAnimatedLabel::SetText(CString strNewText)
{
	SetWindowText(strNewText);
	m_strLabel = strNewText;
	m_bAnimatedDone = FALSE;
	m_bShowSpeedFlag = m_strLabel3.IsEmpty() ? 0 : 1;
	Invalidate();
}

void CAnimatedLabel::SetText3(CString strNewText)
{
	//SetWindowText(m_strLabel);
	m_strLabel3 = strNewText;
	m_bAnimatedDone = FALSE;

	m_bShowSpeedFlag = m_strLabel3.IsEmpty() ? 0 : 1;
	Invalidate();
}

void CAnimatedLabel::SetTextEx(CString strNewText, COLORREF clr )
{
	m_clrText = clr;
	SetText(strNewText);
}

void CAnimatedLabel::SetText2(CString strNewText)
{
	if(m_strLabel2.IsEmpty())
		Invalidate(FALSE);

	m_strLabel2 = strNewText;
	CRect rc;
	GetClientRect(rc);
	rc.left = rc.Width() - LABEL_LENGTH;
	InvalidateRect(rc, FALSE);
}

void CAnimatedLabel::SetFont(CFont& newFont)
{
	LOGFONT lf;
	newFont.GetLogFont(&lf);
	m_font.DeleteObject();
	m_font.CreateFontIndirect(&lf);	
}

void CAnimatedLabel::SetFont(LOGFONT lf)
{
	m_font.DeleteObject();
	m_font.CreateFontIndirect(&lf);
}

void CAnimatedLabel::DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/)
{
	
}


void CAnimatedLabel::OnMouseMove(UINT nFlags, CPoint point)
{
	CBitmapBtn::OnMouseMove(nFlags, point);
	if(!m_MouseOnButton)
		Invalidate(FALSE);
}

void CAnimatedLabel::OnRButtonUp(UINT nFlags, CPoint point)
{
	CRect r;
	GetClientRect(&r);
	if(r.PtInRect(point))
	{
		Invalidate(FALSE);
		//CJiaFilmPlayerDlg* pDlg = (CJiaFilmPlayerDlg*) GetParent();
//		pDlg->OnItemRcClick(m_nID, m_strIDName);
	}
	CBitmapBtn::OnRButtonUp(nFlags, point);
}

void CAnimatedLabel::SetTextShowTimer(int nIndex, int nTimer)
{
	m_nAdTextIndex = nIndex;
	SetTimer(IDT_ADTEXTSHOWTIMER, 1000 * nTimer, NULL);
}